<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
$server->soap_defencoding = 'UTF-8'; 
// Initialize WSDL support
$server->configureWSDL('BugfreeService');
$server->wsdl->schemaTargetNamespace = 'http://www.wicresoft.com/';


$server->wsdl->addComplexType(
    'Chapter',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'title' => array('name'=>'title','type'=>'xsd:string'),
        'page' => array('name'=>'page','type'=>'xsd:int')
    )
);
 
$server->wsdl->addComplexType(
    'ChapterArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Chapter[]')
    ),
    'tns:Chapter'
);
 
$server->wsdl->addComplexType(
    'Book',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'author' => array('name'=>'author','type'=>'xsd:string'),
        'title' => array('name'=>'title','type'=>'xsd:string'),
        'numpages' => array('name'=>'numpages','type'=>'xsd:int'),
        'toc' => array('name'=>'toc','type'=>'tns:ChapterArray')
    )
);

$server->register(
    'getBook',
    array('title'=>'xsd:string'),
    array('return'=>'tns:Book')
);

function getBook($title) {
     // Create TOC
     $toc = array();
     $toc[] = array('title' => 'Chapter One', 'page' => 1);
     $toc[] = array('title' => 'Chapter Two', 'page' => 30);
  
     // Create book
     $book = array(
                 'author' => "Jack London",
                 'title' => $title,
                 'numpages' => 42,
                 'toc' => $toc);
  
     return $book;
}






 
$server->wsdl->addComplexType(
    'Bug',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'BugId'       => array('name'=>'BugId',     'type'=>'xsd:int'),
        'Title'       => array('name'=>'Title',     'type'=>'xsd:string'),
        'AssignedTo'  => array('name'=>'AssignedTo','type'=>'xsd:string'),
        'StatusDate'  => array('name'=>'StatusDate','type'=>'xsd:dateTime'),
        'Comment'     => array('name'=>'Comment',   'type'=>'xsd:string')
    )
);

$server->wsdl->addComplexType(
    'BugArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Bug[]')
    ),
    'tns:Bug'
);

$server->register(
    'GetBug',
    array('bugId'   =>'xsd:int'),
    array('return'  =>'tns:Bug')
);


function ExecuteQuery($query)
{
    $mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="xiashang_bugfree"; // 连接数据库用户名
    $mysql_password="p@ssw0rd01!"; // 连接数据库密码
    $mysql_database="xiashang_telecom"; // 数据库的名字
    
    // 连接到数据库
    $conn=mysql_connect($mysql_server_name, $mysql_username,
                        $mysql_password);
    mysql_query("SET NAMES 'utf8'");
                        
    // 执行sql查询
    return mysql_db_query($mysql_database, $query, $conn);
}
function CloseConnection($result)
{
    // 释放资源
    mysql_free_result($result);
    // 关闭连接
    mysql_close();
}

function GetBug($bugId) {
                   
    // 从表中提取信息的sql语句
    $strsql="select BugID,BugTitle,AssignedTo,AssignedDate,ReproSteps from bf_BugInfo where BugID="
      . $bugId;
    // 执行sql查询
    $result = ExecuteQuery($strsql);
    // 获取查询结果
    $row=mysql_fetch_row($result);
    
    if(!$row)
    {
    CloseConnection($result);
    
    
     return NULL;   
    }
    
    /*
   // 定位到第一条记录
    mysql_data_seek($result, 0);
    // 循环取出记录
    while ($row=mysql_fetch_row($result))
    {
      echo "<tr>\n";
      for ($i=0; $i<mysql_num_fields($result); $i++ )
      {
        echo '<td bgcolor="#00FF00">';
        echo "$row[$i]";
        echo '</td>';
      }
      echo "</tr>\n";
    }*/
    
    
     // Get bug from MySql database
     $bug = array(
                 'BugId'      => $row[0],
                 'Title'      => $row[1],
                 'AssignedTo' => $row[2],
                 'StatusDate' => UTC($row[3]),
                 'Comment'    => $row[4]
                 );
 
 
     CloseConnection($result);
    
    
     return $bug;
}

$server->register(
    'GetBugsByKeyword',
    array('keyword' =>'xsd:string'),
    array('return'  =>'tns:BugArray')
);

function GetBugsByKeyword($keyword) {

    $bugList = array();

    // 从表中提取信息的sql语句
    $strsql="select BugID,BugTitle,AssignedTo,AssignedDate,ReproSteps"
      . " from bf_BugInfo where BugKeyword like '%"
      . $keyword
      . "%'";
    // 执行sql查询
    $result = ExecuteQuery($strsql);

    
    // 循环取出记录
    while ($row=mysql_fetch_row($result))
    {
           $bug = array(
                 'BugId'      => $row[0],
                 'Title'      => $row[1],
                 'AssignedTo' => $row[2],
                 'StatusDate' => UTC($row[3]),
                 'Comment'    => $row[4]
                 );
                 
        array_push($bugList, $bug);         
    }

    CloseConnection($result);
  
     return $bugList;
}

function UTC($date) {
  return date_create($date)->format('Y-m-d\TH:i:s\Z');
}

// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service(utf8_encode($HTTP_RAW_POST_DATA));
?>